#工程的名称及最后生成文件的名字
TARGET = LED_project

#设定临时性环境变量
GCC_POS=/home/z/gcc-arm-none-eabi-9-2020-q2-update/bin/
GCC_POS=$(seq 5 | grep 7)

export CC             = $(GCC_POS)arm-none-eabi-gcc
export AS             = $(GCC_POS)arm-none-eabi-as
export LD             = $(GCC_POS)arm-none-eabi-ld
export OBJCOPY        = $(GCC_POS)arm-none-eabi-objcopy
export OBJDUMP        = $(GCC_POS)arm-none-eabi-objdump

#读取当前工作目录
TOP=$(shell pwd)

#设定包含文件目录
INC_FLAGS= -I $(TOP)/CORE                  \
           -I $(TOP)/HARDWARE    \
           -I $(TOP)/HARDWARE/6050DMP    \
           -I $(TOP)/HARDWARE/OLED \
           -I $(TOP)/Libraries/FWlib/inc \
           -I $(TOP)/Libraries/CMSIS \
           -I ./DRV/delay/ \
           -I ./DRV/NVIC/ \
           -I ./DRV/GPIO/ \
           -I ./DRV/iic/ \
           -I ./DRV/usart/ \
           -I $(TOP)/SYSTEM        \
           -I $(TOP)/USER

CFLAGS = -ggdb -W -Wall  -mcpu=cortex-m3 -mthumb -D STM32F10X_HD -D USE_STDPERIPH_DRIVER $(INC_FLAGS) -O0 -std=gnu11
#C_SRC=$(shell find ./ -name '*.c' | grep -v main.*c )
#C_SRC=$(shell find ./ -name '*.c' | grep -v main.*c | sed '$amain.c' )
C_SRC=$(shell  find ./ -name '*.c' | grep -v mother)
C_OBJ=$(C_SRC:%.c=%.o)          

.PHONY: all clean update      

all:$(C_OBJ)
	$(CC) $(C_OBJ) -g -T stm32_f103ze_gcc.ld -o $(TARGET).elf   -mthumb -mcpu=cortex-m3 -Wl,--start-group -lc -lm -Wl,--end-group -specs=nano.specs -specs=nosys.specs -static -Wl,-cref,-u,Reset_Handler -Wl,-Map=Project.map -Wl,--gc-sections -Wl,--defsym=malloc_getpagesize_P=0x80 
	$(OBJCOPY) $(TARGET).elf  $(TARGET).bin -Obinary 
	$(OBJCOPY) $(TARGET).elf  $(TARGET).hex -Oihex
	$(OBJDUMP) -S $(TARGET).elf > $(TARGET).asm

$(C_OBJ):%.o:%.c
	$(CC) -c $(CFLAGS) -o $@ $<
clean:
	rm -f $(shell find ./ -name '*.o')
	rm -f $(shell find ./ -name '*.d')
	rm -f $(shell find ./ -name '*.map')
	rm -f $(shell find ./ -name '*.elf')
	rm -f $(shell find ./ -name '*.bin')
	rm -f $(shell find ./ -name '*.hex')
	rm -f $(shell find ./ -name '*.asm')

gdbGuochanKali:
	openocd -f /usr/share/openocd/scripts/interface/stlink-v2.cfg  -f guochankali.cfg -c init -c halt 
gdbGuochan:
	openocd -f /usr/share/openocd/scripts/interface/stlink-v2.cfg  -f guochan.cfg -c init -c halt 
gdb:
	openocd -f /usr/share/openocd/scripts/interface/stlink-v2.cfg  -f /usr/share/openocd/scripts/target/stm32f1x_stlink.cfg -c init -c halt 
update:
	openocd -f /usr/share/openocd/scripts/interface/stlink-v2.cfg  -f /usr/share/openocd/scripts/target/stm32f1x_stlink.cfg -c init -c halt -c "flash write_image erase $(TOP)/LED_project.hex" -c reset 
	#openocd -f /usr/share/openocd/scripts/interface/stlink-v2.cfg  -f /usr/share/openocd/scripts/target/stm32f1x_stlink.cfg -c init -c halt -c "flash write_image erase $(TOP)/LED_project.hex" -c reset -c shutdown
guochanupdate:
	openocd -f /usr/share/openocd/scripts/interface/stlink-v2.cfg  -f guochan.f1x.cfg -c init -c halt -c "flash write_image erase $(TOP)/LED_project.hex" -c reset 
